#!/bin/bash

# 🚀 NPM 发布脚本 - @roasmax/rabbitmq
# 使用方法: ./scripts/publish.sh

set -e  # 遇到错误立即退出

echo "🚀 开始发布 @roasmax/rabbitmq 到 NPM..."
echo "================================================"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 1. 检查 npm 登录状态
print_status "检查 NPM 登录状态..."
if npm whoami > /dev/null 2>&1; then
    NPM_USER=$(npm whoami)
    print_success "已登录 NPM，用户: $NPM_USER"
else
    print_error "未登录 NPM！"
    echo ""
    echo "请先登录 NPM："
    echo "  npm login"
    echo ""
    echo "如果没有账户，请先注册："
    echo "  npm adduser"
    echo ""
    exit 1
fi

# 2. 检查当前分支
print_status "检查 Git 分支..."
CURRENT_BRANCH=$(git branch --show-current)
if [ "$CURRENT_BRANCH" != "master" ] && [ "$CURRENT_BRANCH" != "main" ]; then
    print_warning "当前不在主分支 ($CURRENT_BRANCH)，建议切换到 main/master 分支"
    read -p "是否继续发布？(y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        print_error "发布已取消"
        exit 1
    fi
fi

# 3. 检查工作目录状态
print_status "检查 Git 工作目录状态..."
if [ -n "$(git status --porcelain)" ]; then
    print_warning "工作目录有未提交的更改"
    git status --short
    read -p "是否继续发布？(y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        print_error "发布已取消"
        exit 1
    fi
fi

# 4. 运行发布前检查
print_status "运行发布前检查..."
if npx tsx scripts/pre-publish-check.ts; then
    print_success "发布前检查通过"
else
    print_error "发布前检查失败"
    exit 1
fi

# 5. 清理和构建
print_status "清理旧的构建文件..."
pnpm run clean

print_status "构建项目..."
pnpm run build

# 6. 运行测试
print_status "运行测试套件..."
if pnpm test; then
    print_success "所有测试通过"
else
    print_error "测试失败"
    exit 1
fi

# 7. 显示包信息
print_status "包信息预览..."
PACKAGE_NAME=$(node -p "require('./package.json').name")
PACKAGE_VERSION=$(node -p "require('./package.json').version")
echo "  包名: $PACKAGE_NAME"
echo "  版本: $PACKAGE_VERSION"
echo "  描述: $(node -p "require('./package.json').description")"

# 8. 最终确认
echo ""
print_warning "即将发布到 NPM！"
echo "包名: $PACKAGE_NAME"
echo "版本: $PACKAGE_VERSION"
echo "用户: $NPM_USER"
echo ""
read -p "确认发布？(y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    print_error "发布已取消"
    exit 1
fi

# 9. 发布到 NPM
print_status "发布到 NPM..."
if npm publish; then
    print_success "发布成功！"
else
    print_error "发布失败"
    exit 1
fi

# 10. 验证发布
print_status "验证发布..."
sleep 5  # 等待 NPM 更新
if npm view $PACKAGE_NAME version > /dev/null 2>&1; then
    PUBLISHED_VERSION=$(npm view $PACKAGE_NAME version)
    if [ "$PUBLISHED_VERSION" = "$PACKAGE_VERSION" ]; then
        print_success "发布验证成功！版本 $PUBLISHED_VERSION 已在 NPM 上可用"
    else
        print_warning "版本不匹配。本地: $PACKAGE_VERSION, NPM: $PUBLISHED_VERSION"
    fi
else
    print_warning "无法验证发布状态，请手动检查"
fi

# 11. 显示后续步骤
echo ""
echo "🎉 发布完成！"
echo "================================================"
echo ""
echo "📦 包信息:"
echo "  NPM 页面: https://www.npmjs.com/package/$PACKAGE_NAME"
echo "  安装命令: npm install $PACKAGE_NAME"
echo ""
echo "📋 后续步骤:"
echo "  1. 验证安装: mkdir test && cd test && npm init -y && npm install $PACKAGE_NAME"
echo "  2. 更新文档中的版本信息"
echo "  3. 创建 Git 标签: git tag v$PACKAGE_VERSION && git push origin v$PACKAGE_VERSION"
echo "  4. 在 GitHub 上创建 Release"
echo "  5. 更新 CHANGELOG.md"
echo ""
print_success "发布流程完成！"
